$(function () {

    //0 显示批次信息复选框全选事件
    $("#phyBatchInformationFather").click(function () {
        var isChecked = $(this).prop("checked")
        if (isChecked) {
            $(".phyBatchInformationSon").prop("checked", true);
        } else {
            $(".phyBatchInformationSon").prop("checked", false);
        }
    });

    //1 体检批次界面 获取所有批次信息
    $.ajax({
        url: "/hospital/phyexam/all.action",
        type: "GET",
        dataType: "json",
        success: function (data) {
            var $list = $("#tb_6");
            $.each(data, function (index, ele) {
                var $item = createDataItem(index, ele);
                $list.append($item);
            });
        },
        error: function () {
            alert("获取数据失败");
        }
    });

    //1.1 动态创建一条数据
    function createDataItem(index, data) {
        var $item = $("" + "<tr>\n" +
            "            <td style=\"width:30px;\"><input class=\"phyBatchInformationSon\" type=\"checkbox\"></td>\n" +
            "            <td>" + data.phyExmBatchName + "</td>\n" +
            "            <td>" + data.phyExmBatchBeginTime + "</td>\n" +
            "            <td>" + data.phyExmBatchOverTime + "</td>\n" +
            "            <td>" + data.proName + "</td>\n" +
            "            <td>\n" +
            "                <button pro-id=\"" + data.proId + "\" data-id=\"" + data.id + "\" class=\"phyBatchInformationUpdateBtn\">编辑</button>\n" +
            "                <button pro-id=\"" + data.proId + "\" data-id=\"" + data.id + "\" class=\"phyBatchInformationCancelBtn\">删除</button>\n" +
            "            </td>\n" +
            "        </tr>");
        return $item;
    }

    //2 体检批次界面 添加批次按钮监听
    $("#createNewBatchBtn").click(function () {
        $(".phyNewBatchAddInput").css("display", "block");
        $(".phyBatchInformation").css("display", "none");
        //2.1 获取所有项目列表
        $.ajax({
            url: "/hospital/project/all.action",
            type: "GET",
            dataType: "json",
            success: function (data) {
                var $prolist = $("#tb_5");
                $.each(data, function (index, ele) {
                    var $item = $("" + " <tr >\n" +
                        "                <td style=\"width:80px;\">\n" +
                        "                    <button class=\"projectNameShowSon\" data-id=\""+ele.id+"\">添加</button>\n" +
                        "                </td>\n" +
                        "                <td>"+ele.proName+"</td>\n" +
                        "            </tr>");
                    $prolist.append($item);
                });
            },
            error: function () {
                alert("获取项目失败");
            }
        });
    });

    //2.1.1 添加批次界面 项目列表复选框全选事件
    $("#projectNameShowFather").click(function () {
        var isChecked = $(this).prop("checked")
        if (isChecked) {
            $(".projectNameShowSon").prop("checked", true);
        } else {
            $(".projectNameShowSon").prop("checked", false);
        }
    });
    //*********************************************************************************************
    //2.1.2 添加批次界面 项目添加按钮点击事件
    $("#tb_5").delegate(".projectNameShowSon", "click", function () {
        //保存 添加的体检项目的id
        var value = $(this).attr("data-id");
        //2.2 添加批次界面 确认按钮
        $("#phyNewBatchAddInputSureBtn").click(function () {
            // alert(value);
            //获取数入的值
            var batName = $("#phyNewBatchInputName").val();
            var begin = $("#phyNewBatchBeginTime").val();
            var over = $("#phyNewBatchEndTime").val();
            $.ajax({
                url: "/hospital/phyexam/add.action",
                type: "POST",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    "phyExmBatchName": batName,
                    "phyExmBatchBeginTime": begin,
                    "phyExmBatchOverTime": over
                }),
                success: function (data) {
                    for (var i = 0; i < value.length; i++) {
                        $.ajax({
                            url: "",
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json;charset=utf-8",
                            data: JSON.stringify({"rltPhyProId": value[i]}),
                            success: function (data) {
                                //发送数据成功，返回成功数据，隐藏新增信息界面，显示表格数据
                                var $list = $("#tb_6");
                                //1.清空表格数据
                                $("#tb_6 tr:not(:first)").html("");
                                //2.动态创建数据
                                $.each(data, function (index, ele) {
                                    var $item = createDataItem(index, ele);
                                    $list.append($item);
                                });
                            },
                            error: function () {
                                alert("添加项目失败")
                            }
                        });
                    }
                    alert("添加成功");
                    $(".phyNewBatchAddInput").css("display", "none");
                    $(".phyBatchInformation").css("display", "block");
                },
                error: function () {
                    alert("添加批次失败");
                }
            });
        });
    });
    //2.3 添加批次界面 返回按钮事件
    $("#phyNewBatchAddInputBackBtn").click(function () {
        $(".phyNewBatchAddInput").css("display", "none");
        $(".phyBatchInformation").css("display", "block");
    });
    //2.4 添加批次界面 输入框失去焦点
    $("#phyNewBatchInputName").on("blur", function () {
        var batchName = $("#phyNewBatchInputName").val();
        if ($.trim(batchName).length == 0) {
            $("#judgeName").html("/*批次名称不能为空*/");
        }
        else {
            $("#judgeName").remove();
        }
    });
    $("#phyNewBatchBeginTime").on("blur", function () {
        var beginTime = $(this).val();
        if ($.trim(beginTime).length == 0) {
            $("#judgeBeginTime").html("/*开始时间不能为空*/");
        } else {
            $("#judgeBeginTime").remove();
        }
    });
    $("#phyNewBatchEndTime").on("blur", function () {
        var overTime = $(this).val();
        if ($.trim(overTime).length == 0) {
            $("#judgeOverTime").html("/*结束时间不能为空*/");
        } else {
            $("#judgeOverTime").remove();
        }
    });

    //3 体检批次界面 查询按钮监听
    $("#phyNewBatchOperSelectBtn").click(function () {
        var batchName = $("#phyNewBatchOperSelect").val();
        $.ajax({
            url: "/hospital/phyexam/batch.action",
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({"phyExmBatchName": batchName}),
            success: function (data) {
                //查询成功，返回成功数据
                var $list = $("#tb_6");
                //1.清空表格数据
                $("#tb_6 tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item = createDataItem(index, ele);
                    $list.append($item);
                });
            },
            error: function () {
                alert("查询失败");
            }
        });
    });
//***************************************************编辑界面 项目id获取*******************************************
    //4 体检批次界面操作栏 按钮的事件委托
    //4.1 操作栏 编辑按钮事件委托
    $("#tb_6").delegate(".phyBatchInformationUpdateBtn", "click", function () {
        //获取当前批次记录的 id
        var id = $(this).attr("data-id");
        var proId = $(this).attr("pro-id");
        //4.1.1  获取修改前的数据
        var $value = $(this).parents("tr")[0];
        var num1 = $value.children[1].innerHTML;
        var num2 = $value.children[2].innerHTML;
        var num3 = $value.children[3].innerHTML;
        var num4 = $value.children[4].innerHTML;
        $("#getPhytName").val(num1);
        $("#getPhytBeginTime").val(num2);
        $("#getPhytEndTime").val(num3);
        $("#getPhytPorjectName").val(num4);
        //显示编辑界面
        $(".clickUpdateGetInfo").css("display", "block");
        $("#over").css("display", "block");
        //4.1.2 编辑界面 确定按钮点击
        $("#clickUpdateGetInfoSureBtn").unbind("click").click(function () {
            //获取用户修改后的值
            var batchName = $("#getBatchName").val();
            var begin = $("#getPhyProjectBeginTime").val();
            var over = $("#getPhyProjectEndTime").val();
            var proName = $("#getPhytPorjectName").val();

            //发送数据 修改批次信息
            $.ajax({
                url: "/hospital/phyexam/update.action",
                type: "POST",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    "id": id,
                    "proId": proId,
                    "phyExmBatchName": batchName,
                    "phyExmBatchBeginTime": begin,
                    "phyExmBatchOverTime": over,
                    "proName": proName
                }),
                success: function (data) {
                    //编辑成功，返回成功数据
                    var $list = $("#tb_6");
                    //1.清空表格数据
                    $("#tb_6 tr:not(:first)").html("");
                    //2.动态创建数据
                    $.each(data, function (index, ele) {
                        var $item = createDataItem(index, ele);
                        $list.append($item);
                    });
                    alert("修改成功");
                    //隐藏编辑界面
                    $(".clickUpdateGetInfo").css("display", "none");
                    $("#over").css("display", "none");
                },
                error: function () {
                    alert("编辑失败");
                }
            });
        });
    });

    //4.1.3 编辑界面 返回按钮点击
    $("#clickUpdateGetInfoBackBtn").click(function () {
        $(".clickUpdateGetInfo").css("display", "none");
        $("#over").css("display", "none");
    });

    // 4.2 操作栏 删除按钮事件委托
    $("#tb_6").delegate(".phyBatchInformationCancelBtn", "click", function () {
        //获取当前体检批次 id
        var id = $(this).attr("data-id");
        var proId = $(this).attr("pro-id");
        $.ajax({
            url: "/hospital/phyexam/delete.action",
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({"id": id,"proId":proId}),
            success: function (data) {
                //删除成功，返回成功数据
                var $list = $("#tb_6");
                //1.清空表格数据
                $("#tb_6 tr:not(:first)").html("");
                //2.动态创建数据
                $.each(data, function (index, ele) {
                    var $item = createDataItem(index, ele);
                    $list.append($item);
                });
                alert("删除成功");
            },
            error: function () {
                alert("删除失败");
            }
        });
    });
});